﻿/* Copyright (C) Siemens AG, 2012. All Rights Reserved.
 * Author: Roland Eckl, Corporate Technology
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System.Collections.ObjectModel;
using System.Windows.Input;
using RVM;


namespace RvmShared
{
    /// <summary>
    /// Simple test ViewModel
    /// </summary>
    public class SimpleViewModel : RemotableViewModel
    {
        #region Private Members

        /// <summary>
        /// Some magic counter. If the value reaches 2 or above, the action SomeAction shall no longer be executable.
        /// </summary>
        private int _counter = 0;

        #endregion

        #region Public Members

        /// <summary>
        /// Constructor registers ViewModel at registry.
        /// All registered ViewModels are automatically exported.
        /// </summary>
        public SimpleViewModel(bool register)
        {
            if(register)
                ExportViewModel(GetType().ToString());
        }

        /// <summary>
        /// Default constructor, explicit export required.
        /// </summary>
        public SimpleViewModel() : this(false) {}

        /// <summary>
        /// Simple property.
        /// </summary>
        public string Id
        {
            get { return GetValue("Id") as string; }
            set { SetValue("Id", value); }
        }

        /// <summary>
        /// Simple property.
        /// </summary>
        public string Message
        {
            get { return GetValue("Message") as string; } 
            set { SetValue("Message", value); } 
        }

        /// <summary>
        /// Simple command
        /// </summary>
        public ICommand SomeAction
        {
            get { return CreateCommand("SomeAction", SomeActionExecute, CanSomeActionExecute); }
        }

        /// <summary>
        /// Some collection of strings.
        /// </summary>
        public ObservableCollection<string> Nerds
        {
            get { return CreateCollection<string>("Nerds"); }
        }

        /// <summary>
        /// Nested ViewModel implementing INotifyPropertyChanged.
        /// </summary>
        public MinimalViewModel NestedTraditional
        {
            get { return GetValue("NestedTraditional") as MinimalViewModel; }
            set { SetValue("NestedTraditional", value); }
        }

        /// <summary>
        /// Nested ViewModel derieved from RemotableViewModel.
        /// </summary>
        public MinimalRemotableViewModel NestedSimple
        {
            get { return GetValue("NestedSimple") as MinimalRemotableViewModel; }
            set { SetValue("NestedSimple", value); }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Action that adds some text to the Message property.
        /// </summary>
        /// <param name="arg"></param>
        private void SomeActionExecute(object arg)
        {
            // This is an additional gate (optional)
            if (!CanSomeActionExecute(null))
                return;

            if (_counter < 3)
                Nerds.Add("bar");
            else if (_counter < 6)
                Nerds.RemoveAt(0);

            Message += " foo";
            _counter++;
        }

        /// <summary>
        /// Checks wether SomeAction can still be executed.
        /// </summary>
        /// <param name="arg"></param>
        /// <returns>TRUE, if magic counter is below 2</returns>
        private bool CanSomeActionExecute(object arg)
        {
            return (_counter < 7);
        }

        #endregion
    }
}
